package com.memorywords.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.memorywords.mapper.LikeMapper;
import com.memorywords.pojo.PageResult;
import com.memorywords.pojo.Result;
import com.memorywords.pojo.Word;
import com.memorywords.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Override
    public boolean check(int userId, int wordId) {
        return likeMapper.check(userId, wordId);
    }

    @Override
    public void change(int userId, int wordId, int status) {
        if(status==0){
            likeMapper.add(userId, wordId);
        }else{
            likeMapper.del(userId, wordId);
        }
    }

    @Override
    public PageResult getList(int userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Word> page = likeMapper.getList(userId, pageNum, pageSize);

        long total = page.getTotal();
        List<Word> list = page.getResult();

        return new PageResult(total, list);
    }
}
